package demo03;

public class FizzBuzz {
	Object lock = new Object();

	 private int n;
	 private int current = 1;

	
	public FizzBuzz(int n) {
		super();
		this.n = n;
	}

	public void fizz() {
        while (current <= n) {
            synchronized (this) {
                if (current % 3 == 0 && current % 5 != 0) {
                    System.out.print("fizz");
                    current++;
                    notifyAll();
                } else {
                    try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
    }

    public void buzz() {
        while (current <= n) {
            synchronized (this) {
                if (current % 5 == 0 && current % 3 != 0) {
                    System.out.print("buzz");
                    current++;
                    notifyAll();
                } else {
                    try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
    }

    public void fizzbuzz() {
        while (current <= n) {
            synchronized (this) {
                if (current % 3 == 0 && current % 5 == 0) {
                    System.out.print("fizzbuzz");
                    current++;
                    notifyAll();
                } else {
                    try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
    }
    
    public void number() {
        while (current <= n) {
            synchronized (this) {
                if (current % 3 != 0 && current % 5 != 0) {
                    System.out.print(current);
                    current++;
                    notifyAll();
                } else {
                    try {
						wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                }
            }
        }
    }
	public int getN() {
		return n;
	}
	public void setN(int n) {
		this.n = n;
	}

	public Object getLock() {
		return lock;
	}

	public void setLock(Object lock) {
		this.lock = lock;
	}      
	
}
